<!--
 * @Description: 查看
 * @Author: Huang Junjie
 * @Date: 2021-04-13 17:05:40
 * @LastEditTime: 2023-02-28 14:52:45
 * @LastEditors: zhangweili zhang_weili@uxsino.com
-->
<template>
  <div>
    <base-dialog
      width="980px"
      class="dialog_box"
      :visible.sync="dialog.visible"
      :title="dialog.title"
      :loading="dialog.loading"
      :disabled="dialog.disabled"
      :show-default-footer="false"
      @open="beforeOpenDialog">
      <el-row>
        <div class="chart-wrapper">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span class="dm-bold">{{ product.productName }}</span>
              <span class="dmc-sub-title dm-pl8 dmc-dev-amount"> ID: {{ product.id }}</span>
              <span class="dmc-sub-title dm-pl8 dmc-dev-amount"> 标识: {{ product.productIdentification }}</span>
              <el-button
                icon="el-icon-edit"
                plain
                size="mini"
                style="float: right;"
                type="danger"
                @click="handleUpdate">
                更新产品信息
              </el-button>
            </div>
            <el-form label-width="120px">
              <el-row style="display: flex;justify-content: space-around;">
                <el-col :span="10">
                  <el-form-item label="产品名称：">{{ product.productName }}</el-form-item>
                  <el-form-item label="产品型号：">{{ product.model }}</el-form-item>
                  <el-form-item label="厂商ID：">{{ product.manufacturerId }}</el-form-item>
                </el-col>
                <el-col :span="10">
                  <el-form-item label="产品状态：">{{ product.stateName }}</el-form-item>
                  <el-form-item label="数据格式：">{{ product.dataFormat }}</el-form-item>
                  <el-form-item label="厂商名称：">{{ product.manufacturerName }}</el-form-item>
                </el-col>
                <el-col :span="10">
                  <el-form-item label="协议类型：">{{ product.protocolType }}</el-form-item>
                  <el-form-item label="产品描述：">{{ product.remark }}</el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </el-card>
        </div>
      </el-row>
<!--      <el-row>-->
<!--        <div class="chart-wrapper">-->
<!--          <el-card class="box-card">-->
<!--            <el-tabs v-model="activeName">-->
<!--              <el-tab-pane label="模型定义" name="first" style="width:100%;height: 100%;">-->
<!--                <div v-if="this.product.productIdentification" style="width:100%;height:100%">-->
<!--                  <ProductServices :product-identification="this.product.productIdentification" />-->
<!--                </div>-->
<!--              </el-tab-pane>-->
<!--              &lt;!&ndash;              <el-tab-pane label="设备调试" name="second" style="width:100%;background: #F1F6FA;">&ndash;&gt;-->
<!--              &lt;!&ndash;                <img style="width: 100%;" src="@/assets/images/equipmentDebugging.jpg" alt="">&ndash;&gt;-->
<!--              &lt;!&ndash;              </el-tab-pane>&ndash;&gt;-->
<!--            </el-tabs>-->
<!--          </el-card>-->
<!--        </div>-->
<!--      </el-row>-->
    </base-dialog>

    <!-- 添加或修改产品管理对话框 -->
    <el-dialog :close-on-click-modal="false" :title="updateDialog.title" :visible.sync="updateDialog.open" append-to-body width="500px">
      <el-form ref="form" :model="updateDialog.form" :rules="updateDialog.rules" label-width="120px">
        <el-form-item label="产品名称" prop="productName">
          <el-col :span="22">
            <el-input v-model="updateDialog.form.productName" placeholder="请输入产品名称" />
          </el-col>
          <el-col :span="2" style="padding-left: 5px">
            <el-tooltip class="item" content="自定义，支持中文、英文大小写、数字、下划线和中划线" effect="light" placement="right-start">
              <i class="el-icon-question" />
            </el-tooltip>
          </el-col>
        </el-form-item>
        <el-form-item label="厂商ID" prop="manufacturerId">
          <el-col :span="22">
            <el-input v-model="updateDialog.form.manufacturerId" placeholder="请输入厂商ID" />
          </el-col>
          <el-col :span="2" style="padding-left: 5px">
            <el-tooltip class="item" content="支持英文大小写，数字，下划线和中划线" effect="light" placement="right-start">
              <i class="el-icon-question" />
            </el-tooltip>
          </el-col>
        </el-form-item>
        <el-form-item label="厂商名称" prop="manufacturerName">
          <el-col :span="22">
            <el-input v-model="updateDialog.form.manufacturerName" placeholder="请输入厂商名称" />
          </el-col>
          <el-col :span="2" style="padding-left: 5px">
            <el-tooltip class="item" content="支持英文大小写，数字，下划线和中划线" effect="light" placement="right-start">
              <i class="el-icon-question" />
            </el-tooltip>
          </el-col>
        </el-form-item>
        <el-form-item label="产品描述" prop="remark">
          <el-input v-model="updateDialog.form.remark" placeholder="请输入产品描述" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改产品管理对话框 -->
  </div>
</template>

<script>
import { getProductById, updateProduct } from '@/api/cms/monitor/product'
// import ProductServices from './ProductServices'

export default {
  // components: { ProductServices },
  props: {
    dialog: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      // 查看页面数据
      product: null,
      updateDialog: {
        title: null,
        open: false,
        // 表单参数
        form: {
          id: null,
          productName: null,
          manufacturerId: null,
          manufacturerName: null,
          remark: null
        },
        // 表单校验
        rules: {
          productName: [
            { required: true, message: '产品名称不能为空', trigger: 'blur' },
            { min: 2, max: 64, message: '产品名称长度必须介于 2 和 64 之间', trigger: 'blur' },
            {
              // pattern: /^(?!_)(?!.*?_$)(?!-)(?!.*?-$)[\u4e00-\u9fa5a-zA-Z0-9_-]+$/,
              // message: "中文、英文大小写、数字、下划线和中划线，不能以下划线中划线开头和结尾，长度[2,64]",
              pattern: /^[\u4e00-\u9fa5a-zA-Z0-9_-]+$/,
              message: '中文、英文大小写、数字、下划线和中划线，长度[2,64]',
              trigger: 'blur'
            }
          ],
          manufacturerId: [
            { required: true, message: '厂商ID不能为空', trigger: 'blur' },
            { min: 2, max: 50, message: '厂商ID长度必须介于 2 和 50 之间', trigger: 'blur' },
            {
              pattern: /^[a-zA-Z0-9_-]+$/,
              message: '英文大小写、数字、下划线和中划线，长度[2,50]',
              trigger: 'blur'
            }
          ],
          manufacturerName: [
            { required: true, message: '厂商名称不能为空', trigger: 'blur' },
            { min: 2, max: 64, message: '厂商名称长度必须介于 2 和 64 之间', trigger: 'blur' },
            {
              pattern: /^[\u4e00-\u9fa5a-zA-Z0-9_-]+$/,
              message: '中文、英文大小写、数字、下划线和中划线，长度[2,64]',
              trigger: 'blur'
            }
          ]
        }
      }
    }
  },
  watch: {
    'dialog.visible'() {
      if (this.dialog.visible) {
        this.product = null
        this.getDetail(this.dialog.productId)
      }
    }
  },
  methods: {
    getDetail(id) {
      getProductById({ 'id': id }).then(response => {
        console.log(response.data)
        this.product = response.data
      })
    },

    // 表单重置
    reset() {
      this.updateDialog.form = {
        id: null,
        productName: null,
        manufacturerId: null,
        manufacturerName: null,
        remark: null
      }
    },

    // 取消按钮
    cancel() {
      this.updateDialog.open = false
      this.reset()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.updateDialog.form = this.product
      this.updateDialog.open = true
      this.updateDialog.title = '修改产品'
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.updateDialog.form.id != null) {
            updateProduct(this.updateDialog.form).then((response) => {
              this.updateDialog.open = false
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.getDetail(this.updateDialog.form.id)
            })
          }
        }
      })
    },
    // 打开弹框前执行
    beforeOpenDialog() {}
  }
}
</script>
<style lang="scss" scoped>
  .chart-wrapper {
    background: #fff;
    //padding: 16px 16px 0;
    margin-bottom: 16px;
  }

  .box-card .el-form-item {
    margin-bottom: 0px;
  }

  @media (max-width: 1024px) {
    .chart-wrapper {
      padding: 8px;
    }
  }

  .dmc-sub-title {
    font-size: .75rem;
    color: #8a8e99;
  }

  .dm-font16 {
    margin-left: .5rem;
    padding-left: .5rem;
    border-left: 1px solid #dfe1e6;
  }

  .dm-bold {
    font-weight: 700;
  }

  .dm-p18 {
    padding-left: .5rem !important;
  }

  .dmc-dev-amount {
    margin-left: .5rem;
    padding-left: .5rem;
    border-left: 1px solid #dfe1e6;
  }
</style>

